home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
BUSINESS
/
STATA3.LZH
/
INTRO.TUT
< prev
next >
Wrap
Text File
|
1988-08-30
|
13KB
|
380 lines
set output error
set display page 23
set more 1
#delimit ;
di _n(5) in wh
" ___ ____ ____ ____ ____ tm" _n
" /__ / ____/ / ____/" _n
"___/ / /___/ / /___/ A First Session" _n
"-----------------------------------------------" _n(2) ;
di in gr
"In this tutorial, we will simulate a Stata session. Instead of you entering"
_n
"commands, Stata will read its instructions from disk." _n(2)
"This tutorial can be discontinued at any time by pressing "
in wh "Break" in gr "---on a PC," _n
"hold down "
in wh "Ctrl" in gr " and press " in wh "Break"
in gr "; under Unix, hold down " in wh "Ctrl" in gr " and press "
in wh "C" in gr "." _n
"This will cause the Stata period prompt to appear and Stata will await your" _n
"next command." _n(9) ;
#delimit cr
mac def path
capture run nullfile.tut
if _rc {
mac def path "\stata\"
capture run %path`nullfile.tut
if _rc {
mac def path "/usr/stata/"
capture run %path`nullfile.tut
if _rc {
#delimit ;
di in red
"I cannot find the other tutorial files. I have looked in the current" _n
"directory and in \stata (DOS) or /usr/stata (Unix). Is Stata installed" _n
"correctly?" _n(2)
"In any case, I cannot run the tutorial." ;
#delimit cr
exit
}
}
}
macro define F5 "do %path`contents.tut;"
macro define F6 "do %path`intro.tut;"
#delimit ;
di in gr
"When you are ready to continue, press the space bar." ;
set more 0; more ; set more 1;
drop _all ;
label drop _all ;
di in wh _n(4)
"Sample session" _n
"--------------" _n(2) ;
di in gr
"We have told Stata to read commands from disk rather than from the keyboard,"
_n
"but we have prerecorded only half of this session. Everything we type is" _n
"prerecorded. Stata's responses are not. Stata is obtaining its input from"
_n
"the disk, but is producing results as you watch. This is true of all of our"
_n
"tutorials." _n ;
di in gr
"Stata has extensive on-line help. When we type '"
in wh "help" in gr "' after the Stata period" _n
"prompt, we are issuing a command. Let's see how Stata responds:" _n(10)
in wh ". help" ;
set more 0 ; more ; set more 1 ;
noisily help ;
set more 0 ; more ; set more 1 ;
di in gr _n(2)
_dup(79) in wh "-" in gr _n
"Stata suggests that we type '"
in wh "help help"
in gr "' for more information on how to use" _n
"the help system. We won't do that; we'll leave it for you to try on your" _n
"own." _n ;
di in gr
"Instead, we want to use some data we've already entered and stored on disk." _n
"The data is called auto.dta and the command to load previously stored data" _n
"is "
in wh "use"
in gr ". We could learn more about the "
in wh "use" in gr " command by typing '"
in wh "help use" in gr "'." _n
in gr "We'll leave that for you to try, too." _n
_dup(79) in wh "-" _n ;
di in wh ". use %path`auto" ;
noisily use %path`auto ;
di _n(2)
in wh _dup(79) "-" _n
in gr
"We can find out what's in the data by asking Stata to "
in wh "describe" in gr " it:" _n
in wh _dup(79) "-" _n(2)
". describe" ;
set more 0 ; more ; set more 1 ;
noisily describe ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"We can also look at some of the data. Below, we ask Stata to "
in wh "list" in gr " the first" _n
"observation:" _n
in wh _dup(79) "-" _n(2)
". list in 1" ;
noisily list in 1 ;
di _n(2) _dup(79) in wh "-" in gr _n
"There's much more in this data than we are going to use in this short demon-"
_n
"stration, so to make it easier for you to follow along, we now "
in wh "drop" in gr " the ir-" _n
"relevant variables:" _n
in wh _dup(79) "-" _n(2)
". drop mpg hdroom runk length turn displ gratio" ;
noisily drop mpg hdroom trunk length turn displ gratio ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"Using " in wh "describe" in gr " and " in wh "list"
in gr ", we can see what's left:" _n
in wh _dup(79) "-" _n(2)
". describe" ;
noisily describe ;
di in wh _n ". list in 1" ;
noisily list in 1 ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"The variable rep78 is the Consumer's Report repair rating. For the AMC" _n
"Concord we see that its value is 3. We can find out more about rep78 across"
_n
"all observations by " in wh "summariz" in gr "ing it:" _n
in wh _dup(79) "-" _n(2)
". summarize rep78" ;
noisily summarize rep78 ;
di _n _dup(79) in wh "-" in gr _n
"The variable assumes the values 1 through 5. For your information, 1 is the"
_n
"poorest rating and 5 is the best. "
in wh "summarize" in gr " tells us the mean, standard de-"
_n
"viation, minimum, and maximum. For this kind of categorical variable, a full"
_n
"tabulation may be more revealing:" _n
in wh _dup(79) "-" _n(2)
". tabulate rep78, plot" ;
set more 0 ; more ; set more 1 ;
noisily tabulate rep78, plot ;
di _n(2) _dup(79) in wh "-" in gr _n
"Sometimes, just listing data can be informative. For instance, let's "
in wh "list" _n in gr
"the cars with the worst repair report:" _n
in wh _dup(79) "-" _n(2)
". list if rep78==1" ;
noisily list if rep78==1 ;
di ;
set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"Next, we " in wh "list" in gr " the cars with the best repair record:" _n
in wh _dup(79) "-" _n(2)
". list if rep78==5" ;
noisily list if rep78==5 ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"Perhaps you noticed that this last list is dominated by foreign cars. Do" _n
"domestic and foreign cars have different repair records on average? The" _n
"data contains a variable called 'foreign' which takes on the value 1 if a" _n
"a car is foreign and 0 otherwise. We can use that variable to answer the" _n
"question by making separate tabulations for each kind of car:" _n
in wh _dup(79) "-" _n(2)
". tabulate rep78 foreign" ;
noisily tabulate rep78 foreign ;
di ; set more 0 ; more ; set more 1 ;
di _n(3) _dup(79) in wh "-" in gr _n
"It would be easier to understand this data if the tabulation included the" _n
"column percent, i.e., the percent each repair category is of the overall:" _n
in wh _dup(79) "-" _n(2)
". tabulate rep78 foreign, column" ;
set more 0 ; more ; set more 1 ;
noisily tabulate rep78 foreign, column ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"Perhaps we want to show only the column percent (i.e., suppress the actual" _n
"count), and we want to add the chi-square statistic comparing domestic and" _n
"foreign cars:" _n
in wh _dup(79) "-" _n(2)
". tabulate rep78 foreign, column nofreq chi2" ;
noisily tabulate rep78 foreign, column nofreq chi2 ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"Often there is more than one way to measure the same thing. In this case," _n
"we could also compare the mean repair records of domestic and foreign cars:" _n
in wh _dup(79) "-" _n(2)
". tabulate foreign, summarize(rep78)" ;
noisily tabulate foreign, summarize(rep78) ;
di _n _dup(79) in wh "-" in gr _n
"There are two other variables we are going to look at during this demonstra-"
_n
"tion: weight and price of car. What is the relationship between them? One"
_n
"way to get a quick answer is simply to "
in wh "graph" in gr " one against the other." _n ;
di in gr
"(If you do not have a graphics monitor, this next command won't do anything.)"
_n
in wh _dup(79) "-" _n ;
di in wh ". graph weight price" ;
set more 0 ; more ;
noisily graph weight price ;
set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"The graph revealed that weight increases with price." _n
in wh _dup(79) "-" _n(4) ;
di _dup(79) in wh "-" in gr _n
"For those of you who do not have a graphics monitor, Stata also provides a" _n
"low-resolution "
in wh "plot" in gr " command:" _n
in wh _dup(79) "-" _n(2) ;
di in wh ". plot weight price" ;
set more 0 ; more ; set more 1 ;
noisily plot weight price ;
set more 0 ; more ; set more 1 ;
di _n(9) _dup(79) in wh "-" in gr _n
"In both cases, the graphs revealed that weight increases with price. We" _n
"can also calculate the correlation coefficient between weight and price;" _n
"we expect it to be positive:" _n
in wh _dup(79) "-" _n(2)
". correlate weight price" ;
noisily correlate weight price ;
di; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"Going further, we can run a regression of weight against price:" _n
in wh _dup(79) "-" _n(2)
". regress weight price" ;
noisily regress weight price ;
di ; set more 0 ; more ; set more 1 ;
di _n(2) _dup(79) in wh "-" in gr _n
"We can easily retrieve the residuals from this regression. We tell Stata" _n
"to create a new variable, we'll call it 'resid', defined as the true value" _n
"('weight') minus the predicted value from the regression:" _n
in wh _dup(79) "-" _n(2)
". predict resid, residual" ;
noisily predict resid, residual ;
di _n(2) _dup(79) in wh "-" in gr _n
"After running a regression, Stata stores the names of the variables and" _n
"their coefficients. "
in wh "predict" in gr " uses that information, along with the data," _n
"to construct predictions, residuals, and even influence statistics. Here" _n
"we retrieve the residual." _n ;
di in gr
"Remember that we found a difference between domestic and foreign repair" _n
"records. Does that difference carry over to the weight/price relation-" _n
"ship as well?" _n
in wh _dup(79) "-" _n(3)
"by foreign: summarize weight price resid" ;
set more 0 ; more ; set more 1 ;
noisily by foreign: summarize weight price resid ;
di ; set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"The summary above indicates that it does. The average domestic car's re-" _n
"sidual is positive whereas the average foreign car's is negative." _n
in wh _dup(79) "-" _n ;
set more 0 ; more ; set more 1 ;
di _n(2) _dup(79) in wh "-" in gr _n
"Let's graph weight against price using separate symbols for each variable." _n
"We must create two new variables. One will take on the weight of domestic" _n
"cars and missing value otherwise, whereas the other will take on the weight" _n
"of foreign cars and missing value otherwise." _n
in wh _dup(79) "-" _n(2)
". generate weightd = weight if ~foreign" ;
noisily generate weightd=weight if ~foreign ;
di _n in wh ". generate weightf = weight if foreign" ;
noisily generate weightf=weight if foreign ;
label var weightd "Weight of domestic cars" ;
label var weightf "Weight of foreign cars" ;
di _n(4) _dup(79) in wh "-" in gr _n
"Next, we do our plot; first the version for those with graphic monitors:" _n
in wh _dup(79) "-" _n(2)
". graph weightd weightf price" ;
set more 0 ; more ;
noisily graph weightd weightf price ;
set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"You saw the domestic weight plotted as small circles and the foreign weight" _n
"as triangles." _n
in wh _dup(79) "-" _n(2) ;
di _n(3) _dup(79) in wh "-" in gr _n
"And now the version for those without graphic monitors:" _n
in wh _dup(79) "-" _n(2)
". plot weightd weightf price" ;
set more 0 ; more ; set more 1 ;
noisily plot weightd weightf price ;
set more 0 ; more ; set more 1 ;
di _n _dup(79) in wh "-" in gr _n
"You saw the domestic weights plotted as A's and the foreign weights as B's."
_n(2)
"In either case, the graphs show that domestic car weights are almost uniformly"
_n
"above the foreign car weights at the same price." _n
in wh _dup(79) "-" _n(9) ;
di _n(2) _dup(79) in wh "-" in gr _n
"For those with graphic monitors, here's another way of seeing the same thing."
_n
in wh _dup(79) "-" _n(2)
". graph price weight, by(foreign) total" ;
set more 0 ; more ;
noisily graph price weight, by(foreign) total ;
set more 1 ;
di _n(8) _dup(79) in wh "-" in gr _n
"We can rerun the regression and include an indicator variable for foreign" _n
"cars to find out if the difference is significant." _n
in wh _dup(79) "-" _n(2)
". regress weight price foreign" ;
set more 0 ; more ; set more 1 ;
noisily regress weight price foreign ;
set more 0 ; more ; set more 1 ;
di _n(4) in wh
"Demonstration ends" _n
"------------------" _n ;
macro define F6 "do %path`graphics.tut;" ;
di in gr
"That concludes our short demonstration, but there's much more. We now return"
_n
"control to you. Some suggestions:" _n ;
di in gr
"If you ..." _col(34) "Then we will show you ..." _n
" Press " in wh "F5" in gr _col(38) "a table of tutorial contents" _n
" Press " in wh "F6" in gr _col(38) "the next tutorial, "
in wh "graphics.tut" _n ;
run %path`tobuy.tut ;